#ifndef CONSOLE_H
#define CONSOLE_H

#include "types.h"
#include "tty.h"
#include "mutex.h"

#define CONSOLE_DISP_ADDR 0xb8000
#define CONSOLE_DISP_END (0xb8000+32*1024)
#define CONSOLE_ROW_MAX 25
#define CONSOLE_COL_MAX 80

#define ASCII_ESC 0x1b

#define ESC_PARAM_MAX  10

//SGR select graphic rendition
//10-37,40-47 black,red,green,yellow,blue,magenta,cyan,white
//30-38 set foreground, 39 default white
//40-48 set background
//ESC[Ps;Ps;...;Psm
typedef union _disp_char_t{
    uint16_t v;
    struct {
        char c;
        char foreground : 4;
        char background : 3;
        char : 1;
    };
}disp_char_t;

typedef enum{
    COLOR_Black=0,
    COLOR_Blue,
    COLOR_Green,
    COLOR_Cyan,
    COLOR_Red,
    COLOR_Magenta,
    COLOR_Brown,
    COLOR_Gray,

    COLOR_DarkGray,
    COLOR_Light_Blue,
    COLOR_Light_Green,
    COLOR_Light_Cyan,
    COLOR_Light_Red,
    COLOR_Light_Magenta,
    COLOR_Yellow,
    COLOR_White,
}color_t;

typedef struct _console_t{
    enum{
        CONSOLE_WRITE_NORMAL,
        CONSOLE_WRITE_ESC,
        //deal with esc[ps1;ps2;...psm
        CONSOLE_WRITE_SQUARE,
        CONSOLE_WRITE_E0,
    }write_state;
    disp_char_t * disp_base;
    int cursor_row,cursor_col;
    int display_rows,display_cols;
    color_t foreground,background;
    int old_cursor_row,old_cursor_col;
    //deal with color
    int esc_param[ESC_PARAM_MAX];
    int curr_param_index;
    mutex_t mutex;
}console_t;

int console_init(int console_idx);
int console_write(int console,char * ptr,int size);
void console_close(int console);
int console_write_tty(tty_t * tty);
void console_select(int console_idx);
#endif